<?php
// 二维码管理
namespace Home\Controller;
// use Think\Controller;
class QrcodeController extends BaseController {
	private $action_name = array('QR_LIMIT_SCENE'=>0,'QR_SCENE'=>1);
	public function index(){
		$page = I('page',1);
		$mod = D('Qr_code');
		$where = array('app_id'=>$this->app['id']);
		$count = $mod->where($where)->count();
		$data = $mod->where($where)->page($page.',20')->order('scene_id')->select();
		
		$this->assign('page',$page);
		$this->assign('prev',($page > 1) ? $page-1 : 1);
		$this->assign('next',($page > ceil($count/20)) ? $page+1 : ceil($count/20));
		$this->assign('count',ceil($count/20));
		
		$this->assign('data',$data);
		$this->display();
	}
	
	public function add(){
		$mod = D('Qr_code');
		$data = $this->get_ticket ( array (
				'expire_seconds' => I('expire_seconds',0,'intval'),	//该二维码有效时间，以秒为单位。 最大不超过1800。
				'action_name' =>I('action_name','QR_LIMIT_SCENE'),	//二维码类型，QR_SCENE为临时,QR_LIMIT_SCENE为永久
				'action_info' => array (
						'scene' => array (
								'scene_id' => I('scene_id',0,'intval') 	//场景值ID，临时二维码时为32位非0整型，永久二维码时最大值为100000（目前参数只支持1--100000）
						) 
				) 
		) );
		if(is_string($data)) $this->success($data);
		if(!empty($data['ticket']) && I('scene_id',0,'intval')){
			$data['app_id'] = $this->app['id'];
			$data['action_name'] = $this->action_name[I('action_name','QR_LIMIT_SCENE')];
			$data['expire_seconds'] = I('expire_seconds',0,'intval');
			$data['remark'] = I('remark');
			$data['scene_id'] = I('scene_id',0,'intval');
			$data['addtime'] = time();
			$data = $mod->create($data);
			if($mod->add($data)){
				$this->success('添加成功');
			}else{
				$this->success('添加失败');
			}
		}else{
			$this->success('添加失败');
		}
	}
}
